package com.kcs.fxmargin.service;

import java.sql.Connection;

import com.authen.ldap.KCS_LDAPEntry;
import com.authen.ldap.LDAPUtil;
import com.kcs.common.utils.Based64Utils;
import com.kcs.common.utils.ConnectionUtils;
import com.kcs.common.utils.Constant;
import com.kcs.fxmargin.bo.LoginBO;
import com.kcs.fxmargin.bo.MainPageBO;
import com.kcs.fxmargin.dao.LoginDAO;
import com.kcs.fxmargin.dao.MainPageDAO;

public class LoginService {
	
    public LoginBO authenticateUser(LoginBO model) throws Exception {
        LDAPUtil test = null;
        KCS_LDAPEntry ent = null;
        
        try
        {
            if(validateModel(model))
            {
                test = new LDAPUtil();
                ent = test.authenticateUser(model.getUserId(), model.getPassword(), model.getUserId());
                if(ent != null){
                	model = new LoginBO();
                	model.setUserId(model.getUserId());
                	model.setPassword(Based64Utils.base64encode(model.getPassword()));
                	model.setUserName(model.getUserName());
                	model.setRoleId(model.getRoleId());
                	model.setResultLdap(true);
                }else{
                	model.setResultLdap(false);
                }
            }else{
            	model.setResultLdap(false);
            }
        }
        catch(Exception e)
        {
        	model.setResultLdap(false);
            throw e;
        }
        return model;
    }

    public boolean validateModel(LoginBO model)throws Exception {
        if(model.getUserId().equals("")){
            return false;
        }

        return !model.getPassword().equals("");
    }
    
    public LoginBO getUserData(LoginBO model)throws Exception{
        Connection conn = null;
        LoginDAO dao = null;
        
        try{
            if(validateGetUserData(model)){
                conn = ConnectionUtils.getConnection(Constant.MSSQL, Constant.TO);
                dao = new LoginDAO();
                model = dao.getUserData(model, conn);
            }
        }
        catch(Exception e)
        {
            throw e;
        }finally{
        	if(conn != null){
	            try{
	                conn.close();
	            }catch(Exception ex){
	                throw ex;
	            }
        	}
        }
        return model;
	}
	
	public boolean validateGetUserData(LoginBO model)throws Exception {
		return !model.getUserId().equals("");
	}
	
	public LoginBO getMenuList(LoginBO model) throws Exception {
        Connection conn = null;
        LoginDAO dao = null;
        
        try{
            if(validateGetUserListModel(model)){
                conn = ConnectionUtils.getConnection(Constant.MSSQL, Constant.TO);
                dao = new LoginDAO();
                model = dao.getMenuList(model, conn);
            }else{
            	throw new Exception(Constant.ERR_ROLE_ID_EMPTY);
            }
        }
        catch(Exception e)
        {
            throw e;
        }finally{
        	if(conn != null){
	            try{
	                conn.close();
	            }catch(Exception ex){
	                throw ex;
	            }
        	}
        }
        return model;
    }
	
	public boolean validateGetUserListModel(LoginBO model) throws Exception {
		return !model.getRoleId().equals("");
	}
}